﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MicrosoftGestureController.cs" company="arvystate.net">
//   arvystate.net
// </copyright>
// <summary>
//   Gesture controller uses Microsoft Sample library to recognize swipe gestures
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace PresentationAid.Lib.GestureControllers
{
    using Kinect.Toolbox;

    using Microsoft.Kinect;
    using Microsoft.Samples.Kinect.SwipeGestureRecognizer;

    using NLog;

    using PresentationAid.Lib.Enums;

    /// <summary>
    /// Gesture controller uses Microsoft Sample library to recognize swipe gestures
    /// </summary>
    public class MicrosoftGestureController : DelayGestureController
    {
        #region Static Fields

        /// <summary>
        /// The logger.
        /// </summary>
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        #endregion

        #region Fields

        /// <summary>
        /// The _swipe gesture recognizer.
        /// </summary>
        private readonly Recognizer swipeGestureRecognizer;

        /// <summary>
        /// The _skeletons.
        /// </summary>
        private Skeleton[] skeletons;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MicrosoftGestureController"/> class.
        /// </summary>
        public MicrosoftGestureController()
        {
            Logger.Trace("Init: MicrosoftGestureController");

            this.swipeGestureRecognizer = this.CreateSwipeRecognizer();
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The recognize.
        /// </summary>
        /// <param name="frame">
        /// The frame.
        /// </param>
        public override void Recognize(SkeletonFrame frame)
        {
            frame.GetSkeletons(ref this.skeletons);

            this.swipeGestureRecognizer.Recognize(new object(), frame, this.skeletons);
        }

        #endregion

        #region Methods

        /// <summary>
        /// The create swipe recognizer.
        /// </summary>
        /// <returns>
        /// The <see cref="Recognizer"/>.
        /// </returns>
        private Recognizer CreateSwipeRecognizer()
        {
            // Instantiate a recognizer.
            Recognizer recognizer = new Recognizer();

            // Wire-up swipe right to manually advance picture.
            recognizer.SwipeRightDetected += (s, e) =>
                {
                    Logger.Debug("EventDetail: GestureRecognized - Gesture: SwipeRight");

                    this.ExecuteGestureRecognized(GestureCommand.SwipeRight, e.Skeleton.TrackingId);
                };

            // Wire-up swipe left to manually reverse picture.
            recognizer.SwipeLeftDetected += (s, e) =>
                {
                    Logger.Debug("EventDetail: GestureRecognized - Gesture: SwipeLeft");

                    this.ExecuteGestureRecognized(GestureCommand.SwipeLeft, e.Skeleton.TrackingId);
                };

            return recognizer;
        }

        #endregion
    }
}